import { ECSEntity } from "../../../3rd/ECS/ECSEntity";
import { ECSSystem } from "../../../3rd/ECS/ECSSystem";
import { ecsclass } from "../../../3rd/ECS/__private";
import { Vector3 } from "../../../Bridge/Vec";
import { App } from "../../../Framework/Core/App";
import { PoolManager } from "../../../Framework/Manager/PoolManager";
import Util from "../../../Framework/Util/Util";
import { GameRT } from "../../GameRT";
import { GameComponent, GameState } from "../components/GameComponent";
import { InputComponent } from "../components/InputComponent";
import { MovableComponent } from "../components/MovableComponent";
import { PlayerComponent } from "../components/NoDataComponent";
import { PositionComponent } from "../components/PositionComponent";
import { RenderNodeComponent } from "../components/RenderNodeComponent";
import { EatSystem } from "./EatSystem";
import { MovableSystem } from "./MovableSystem";
import { StarFactorSystem } from "./StarFactorySystem";

@ecsclass("GameSystem")
export class GameSystem extends ECSSystem {
    
    start()
    {

        var gameEntity = new ECSEntity("game"); 

 
        var gameComponent =  new GameComponent();
        var inputComponent = new InputComponent();

        gameEntity.addComponent(gameComponent);   
        gameEntity.addComponent(inputComponent);   
        gameComponent.gameState = GameState.GameStart; //测试数据  
        
        //玩家实体添加组件
        gameComponent.player.addComponent(new  MovableComponent(GameRT.MoveSpeed));
        gameComponent.player.addComponent(new PositionComponent())
        var playerNode = App.Instance.poolMgr.Get("player");
        gameComponent.player.addComponent(new RenderNodeComponent(playerNode));
        gameComponent.player.addComponent(new PlayerComponent())
        
        //添加系统
        this.ecs.systems.add(gameComponent.moveSys);
        this.ecs.systems.add(gameComponent.starFactorySys);
        this.ecs.systems.add(gameComponent.positionSystem);
        this.ecs.systems.add(gameComponent.barrierFactorySystem);
        this.ecs.systems.add(gameComponent.barrierSystem);
        this.ecs.systems.add(gameComponent.eatSystem);

        //添加游戏实体和玩家实体
        this.ecs.entities.add(gameComponent.player);
        this.ecs.entities.add(gameEntity);
        // this.EnableSys(true);

    }

    public EnableSys(state:boolean)
    {
        // Util.Log("EnableSys")
        
        var gameComponent = this.ecs.components.get(GameComponent);
        gameComponent.moveSys.enabled = state;
        gameComponent.positionSystem.enabled = state;
        gameComponent.starFactorySys.enabled = state;
        gameComponent.eatSystem.enabled = state;
        gameComponent.barrierFactorySystem.enabled = state;
        gameComponent.barrierSystem.enabled = state;

    }


    update()
    {



        var gameComponent = this.ecs.components.get(GameComponent);
        
        if (gameComponent.gameState != gameComponent._gameState) 
        {   
            gameComponent._gameState = gameComponent.gameState;
            if (gameComponent.gameState == GameState.GameStart)
            {
                this.EnableSys(true);
            }
            else 
            {
                this.EnableSys(false);
            }
        }
       
        gameComponent.gameFrame +=1;

        


    }

    unload()
    {
        var gameComponent = this.ecs.components.get(GameComponent);
        var playerNode = gameComponent.player.getComponent(RenderNodeComponent).node;
        playerNode.Put();
    }

}